import { Router, json } from 'express';
import { ApiResponse } from '@src/interface';
import { CustomerService } from '@src/service';
import { expressMiddleware } from '@apollo/server/express4';
import cors from 'cors';
import { ApolloServers } from '@src/apollo';
import { isCustLogin } from '@src/middleware';

export function getCustomerRouter() {
    const customerRouter = Router();

    customerRouter.post('/register', (req, res, next) => {
        console.log(req.body)
        CustomerService.register(req.body).then(() => {
            res.send(<ApiResponse>{
                errcode: 0
            })
        }).catch(next)
    });
    customerRouter.post('/login', (req, res, next) => {
        console.log(req.body)
        CustomerService.loginByPwd(req.body).then((rst) => {
            res.send(<ApiResponse>{
                errcode: 0,
                result: rst.token
            })
        }).catch(next)
    });
    customerRouter.use(
        '/graphql',
        cors(),
        json(),
        // isCustLogin,
        expressMiddleware(ApolloServers.instance.custApolloServer),
    );
    return customerRouter
}